"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var React = require("react");
var ReactDOM = require("react-dom");
var utils_1 = require("./utils");
/**
 * Detects if a passed element is a Fiber object instead of an element. Is needed as `ReactDOM.findDOMNode()` returns
 * a Fiber in `react-test-renderer` that can cause issues with tests. Is used only in non-production env.
 *
 * @see https://github.com/facebook/react/issues/7371#issuecomment-317396864
 * @see https://github.com/Semantic-Org/Semantic-UI-React/issues/4061#issuecomment-694895617
 */
function isFiberRef(node) {
    if (node === null) {
        return false;
    }
    if (node instanceof Element || node instanceof Text) {
        return false;
    }
    return !!(node.type && node.tag);
}
var RefFindNode = /** @class */ (function (_super) {
    tslib_1.__extends(RefFindNode, _super);
    function RefFindNode() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.prevNode = null;
        return _this;
    }
    RefFindNode.prototype.componentDidMount = function () {
        var currentNode = ReactDOM.findDOMNode(this);
        if (process.env.NODE_ENV !== 'production') {
            if (isFiberRef(currentNode)) {
                currentNode = null;
            }
        }
        this.prevNode = currentNode;
        utils_1.handleRef(this.props.innerRef, currentNode);
    };
    RefFindNode.prototype.componentDidUpdate = function (prevProps) {
        var currentNode = ReactDOM.findDOMNode(this);
        if (process.env.NODE_ENV !== 'production') {
            if (isFiberRef(currentNode)) {
                currentNode = null;
            }
        }
        if (this.prevNode !== currentNode) {
            this.prevNode = currentNode;
            utils_1.handleRef(this.props.innerRef, currentNode);
        }
        if (prevProps.innerRef !== this.props.innerRef) {
            utils_1.handleRef(this.props.innerRef, currentNode);
        }
    };
    RefFindNode.prototype.componentWillUnmount = function () {
        utils_1.handleRef(this.props.innerRef, null);
        delete this.prevNode;
    };
    RefFindNode.prototype.render = function () {
        var children = this.props.children;
        return children;
    };
    return RefFindNode;
}(React.Component));
exports.RefFindNode = RefFindNode;
